//
//  DTClassBottomView.m
//  DanTang
//
//  Created by honeyrr on 2017/10/10.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "DTClassBottomView.h"
#import "DTVerticalButton.h"

@interface DTClassBottomView()
@property(nonatomic, strong) NSArray *groups;

@end

@implementation DTClassBottomView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadGroups];
    }
    return self;
}

//加载数据
-(void) loadGroups
{
    NSString *urlString = @"http://api.dantangapp.com/v1/channel_groups/all?";
    [[DTNetWork sharedNetWork] loadDataInfo:urlString parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        self.groups = responseObject[@"data"][@"channel_groups"];
        [self performSelectorOnMainThread:@selector(setUpUI) withObject:nil waitUntilDone:NO];
    } failure:^(NSError * _Nullable error) {
        
    }];
}
//加载ui
-(void)setUpUI
{
    //风格板块
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor whiteColor];
    topView.mr_x = 0;
    topView.mr_y = 10;
    topView.mr_width = DTScreenW;
    [self addSubview:topView];
    
    UILabel *labelTop = [self setUpLabel:@"风格"];
    [topView addSubview:labelTop];
    
    NSArray *topGroups = self.groups[0][@"channels"];
    
    //坐标位置定义
    NSInteger cols = 4;
    CGFloat margin = 10;
    CGFloat width = (self.mr_width - ((cols + 1) * margin))/cols;
    CGFloat height = width /4 * 5;
    //循环创建
    for (NSInteger index = 0; index < topGroups.count; index++) {
        CGFloat x = (index % cols) * (width + margin) + margin;
        
        CGFloat y = (index / cols) * height + CGRectGetMaxY(labelTop.frame) - 10;
        
        //创建自定义button
        DTVerticalButton *btn = [[DTVerticalButton alloc]init];
        //用tag保存btn的ID
        btn.tag = [[topGroups[index] objectForKey:@"id"] integerValue];
        
        [btn addTarget:self action:@selector(groupButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitle:[topGroups[index] objectForKey:@"name"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[topGroups[index] objectForKey:@"icon_url"]]]] forState:UIControlStateNormal];
        
        btn.frame = CGRectMake(x, y, width, height);
        
        [topView addSubview:btn];
        
        if (index == topGroups.count - 1) {
            topView.mr_height = CGRectGetMaxY(btn.frame);
        }
    }
    
    //品类板块
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor =[UIColor whiteColor];
    bottomView.mr_x = 0;
    bottomView.mr_y = CGRectGetMaxY(topView.frame) + 10;//改
    bottomView.mr_width = DTScreenW;
    bottomView.mr_height = self.mr_height - bottomView.mr_y; //加
    [self addSubview:bottomView];
    
    UILabel *labelbottom = [self setUpLabel:@"品类"];
    [bottomView addSubview:labelbottom];
    
    NSArray * bottomGroups = self.groups[1][@"channels"]; //改
    
    //循环创建
    for (NSInteger index = 0; index < bottomGroups.count; index++) {
        CGFloat x = (index % cols) * (width + margin) + margin;
        CGFloat y = (index / cols) * height + CGRectGetMaxY(labelbottom.frame) - 5;
        //创建自定义btn
        DTVerticalButton *btn = [[DTVerticalButton alloc]init];
        //用tag保存Btn的ID
        btn.tag = [[bottomGroups[index] objectForKey:@"id"] integerValue];
        [btn addTarget:self action:@selector(groupButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        //设置btn的字体颜色
        [btn setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6] forState:UIControlStateNormal];
        
        [btn setTitle:[bottomGroups[index] objectForKey:@"name"] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[bottomGroups[index] objectForKey:@"icon_url"]]]] forState:UIControlStateNormal];
        
        btn.frame = CGRectMake(x, y, width, height);
        
        [bottomView addSubview:btn];
        
        if (index == topGroups.count - 1) {
            topView.mr_height = CGRectGetMaxY(btn.frame)- 10;
        }
    }
}

-(void) groupButtonClick:(UIButton *)sender
{
    //判断有没有被实现并调用
    if ([self.delegate respondsToSelector:@selector(groupButtonItemClick:)]) {
        [self.delegate groupButtonItemClick:sender];
    }
}

-(UILabel *)setUpLabel:(NSString *)title
{
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(10, 5, 100, 40);
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:16];
    label.text = title;
    return label;
}

@end
